<template>
  <div id="headers">
    <a-row id="globalHeader" align="center" :wrap="false">
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          @menu-item-click="doMenuClick"
          :selected-keys="selectedKeys"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="titleBar">
              <img class="logo" src="../assets/logo.png" />
              <div class="title">糊途智答</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in showVisibleMenus" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div v-if="currentUser?.id">
          {{ currentUser?.userName ?? "无名" }}
        </div>
        <div v-else>
          <a-button type="primary" href="/user/login">登录</a-button>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRoute, useRouter } from "vue-router";
import { ref } from "vue";
import { useLoginUserStore } from "@/store/userStore";
import API from "@/api";
import { getVisibleMenus } from "@/utils/RouterUtils";
const router = useRouter();
const loginUserStore = useLoginUserStore();
// 获取当前路由
let route = useRoute();
let currentPath = route.path;
// 当前选中的菜单项
const selectedKeys = ref([currentPath]);
// 刷新页面时可以跳转到点击的菜单栏,
router.afterEach((to) => {
  selectedKeys.value = [to.path];
});
const currentUser: API.LoginUserVO | null = loginUserStore.loginUser;
const showVisibleMenus = getVisibleMenus(routes, currentUser);
// 点击菜单跳转到对应页面
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
#globalHeader {
}

.titleBar {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 16px;
  color: black;
}

.logo {
  height: 48px;
}
</style>
